<script setup lang="ts">
import SList from '@/components/common/list/SList.vue'
import { useList } from '@/composables/list-composable'
import type { ResultData } from '@/api/service'
import type { Page } from '@/type/User'
import { permListApi } from '@/api/perm'
import type { PermVo } from '@/type/system/perm-types'
import PermInfo from '@/views/perm/PermInfo.vue'
import { ref } from 'vue'

const viewId = 'PermList'
const { tableData, pageQuery, statement, total, tableLoading, search, hiddenColumn } = useList(
  viewId,
  getData,
  init
)
const permInfoId = ref<string | undefined>()
const permInfoDialog = ref<boolean>(false)

function init() {
  hiddenColumn('s')
}

function getData() {
  tableLoading.value = true
  permListApi(pageQuery).then((response: ResultData<Page<PermVo[]>>) => {
    total.value = response.data.total
    tableData.value = response.data.modelList
    tableLoading.value = false
  })
}

function close() {
  permInfoDialog.value = false
  getData()
}

const methods = {
  edit: (item: PermVo) => {
    permInfoId.value = item.permId
    permInfoDialog.value = true
  },
  add: () => {
    permInfoId.value = undefined
    permInfoDialog.value = true
  }
}
</script>

<template>
  <s-list
    :statement="statement"
    :data="tableData"
    :methods="methods"
    :total="total"
    :table-loading="tableLoading"
    :page-size="pageQuery.pageSize"
    @search="search"
  >
  </s-list>

  <el-dialog
    destroy-on-close
    :close-on-click-modal="false"
    v-model="permInfoDialog"
    title="编辑权限信息"
    width="900"
  >
    <perm-info :close="close" :query="{ permId: permInfoId }" />
  </el-dialog>
</template>

<style scoped></style>
